<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Rpp extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_rpp';

		$this->primary_key = 'aka_rpp.rpp_id';

	}
	function get_all($kelas_id,$semester_id,$pelajaran_id,$jurusan_id,$aktif=null){
	
		$this->load->database();
			$filter = "";
			
			if($jurusan_id == 0){
				$filter = " AND {$this->table_name}.jurusan_id IS NULL";
			}else{
				$filter = " AND {$this->table_name}.jurusan_id='$jurusan_id'";
			}
			
			if($aktif != null)
				$filter .= " AND {$this->table_name}.aktif='$aktif'";
						
				//$sql = "SELECT * FROM {$this->table_name}  where aka_rpp.tingkat_id=aka_tingkat.tingkat_id and aka_rpp.jurusan_id=aka_jurusan.jurusan_id and //aka_rpp.semester_id=aka_semester.semester_id and aka_rpp.pelajaran_id=aka_pelajaran.pelajaran_id ORDER BY {$this->primary_key}";
				//SELECT * FROM aka_rpp WHERE aka_rpp.tingkat_id =12 AND aka_rpp.semester_id =5 AND aka_rpp.pelajaran_id =8 AND aka_rpp.jurusan_id =3 ORDER BY aka_rpp.rpp_id
				 $sql = "select * from aka_rpp where aka_rpp.tingkat_id='$kelas_id' and aka_rpp.semester_id='$semester_id' and aka_rpp.pelajaran_id='$pelajaran_id' $filter order by aka_rpp.rpp_id";
				
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM {$this->table_name} WHERE {$this->primary_key}='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where($this->primary_key,$id);
				$this->db->update($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM {$this->table_name} WHERE {$this->primary_key}='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>